<?php

class AdminProductsController extends AdminAppController {
	
	public $name = 'Products';
	
	public $uses = array('Product');
	
	public function index() {
		$this->Product->recursive = 0;
		$this->paginate = array (
			'order' => array('Product.id' => 'desc')
		);
		$this->set('products', $this->paginate());
	}
	
	public function create() {
		
		if( $this->request->is('post') ) {
			$this->request->data['Picture']['group'] = 'product';
			$this->Product->create();
			if( $this->Product->saveAssociated( $this->request->data ) ) {
				$this->setUserInfo(__d('admin', 'The Product has been created.'), 'success');
				$this->redirect(array('action' => 'index'));
			}
			else {
				$this->setUserInfo(__d('admin', 'The Product could not be saved. Please, try again.'), 'error');
			}
		}
		
		$this->set('users', $this->Product->User->find('list', array('order' => array('User.first_name', 'User.last_name'))));
		$this->set('categories', $this->Product->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;"));
	}
	
	public function view($id) {
		
		$this->set('product', $this->Product->findById($id));
		
	}
	
	public function upload() {
		
		if( $this->request->is('post') ) {
			
			$this->request->data['Resource']['group'] = 'product';
			pr( $this->request->data );
			$this->Product->Resource->Behaviors->load('FileUpload.FileUpload', array(
				'uploadDir' => 'pictures/products',
				'fileVar' => 'picture',
				'fileNameFunction' => 'crc32'
			));
			$this->Product->saveAssociated( $this->request->data );
			
			$product = $this->Product->read();
			pr( $product );
			
			
			//$this->Product->create();
			//$this->Product->save($this->request->data);
			
		}
		
	}
	
}